package com.company.huisuo.biz.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.company.huisuo.base.BaseSingleAdapter
import com.company.huisuo.biz.bean.MerchantAtmosphereBean
import com.company.huisuo.databinding.ItemKroomAuraBinding
import com.company.huisuo.ext.loadImg
import com.company.huisuo.ext.toast

/**
 * 我的K房-氛围组
 */
class KRoomAuraAdapter : BaseSingleAdapter<MerchantAtmosphereBean, ItemKroomAuraBinding>() {

    private var mSelectedPos = 0

    override val initViewBinding: (LayoutInflater, ViewGroup, Boolean) -> ItemKroomAuraBinding
        get() = ItemKroomAuraBinding::inflate

    override fun convert(
        holder: SingleViewHolder<ItemKroomAuraBinding>, position: Int, item: MerchantAtmosphereBean?
    ) {
        item?.let {
            // 加载氛围图片
            holder.binding.ivImage.loadImg(it.atmosphereCover)
            
            // 设置氛围名称
            holder.binding.tvName.text = it.atmosphereName
            
            // 设置选中状态
            val isSelected = mSelectedPos == position || it.selected
            holder.binding.tvButton.isSelected = isSelected
            holder.binding.tvButton.text = if (isSelected) {
                "进行中"
            } else {
                "使用"
            }
            holder.binding.viewBorder.isSelected = isSelected
            
            // 点击事件
            holder.binding.tvButton.setOnClickListener {
                if (mSelectedPos != position) {
                    "选择成功".toast()
                    mSelectedPos = position
                    notifyDataSetChanged()
                }
            }
        }
    }
    
    /**
     * 获取当前选中的氛围
     */
    fun getSelectedAtmosphere(): MerchantAtmosphereBean? {
        return if (mSelectedPos >= 0 && mSelectedPos < itemCount) {
            getItem(mSelectedPos)
        } else {
            null
        }
    }
}